home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Dots & Pixels / toolsources / Transparency.cp < prev   
Text File  |  1995-09-29  |  14KB  |  561 lines

  1. #include <assert.h>
  2.  
  3. #include <Windows.h>
  4. #include <QDOffscreen.h>
  5. #include <Memory.h>
  6. #include <Fonts.h>
  7. #include <Packages.h>
  8. #include <SegLoad.h>
  9. #include <ToolUtils.h>
  10. #include <TextEdit.h>
  11. #include <Files.h>
  12. #include <Palettes.h>
  13.  
  14. #include <Retrace.h>
  15. #include <Devices.h>
  16. #include <Timer.h>
  17. #include <ColorPicker.h>
  18.  
  19. #include "C_randomizer.h"
  20.  
  21. #include "general.h"
  22. #include "port.h"
  23. #include "window.h"
  24. #include "depthchange.h"
  25. #include "fullscreen.h"
  26. #include "gworld.h"
  27. #include "scrolling_noise.h"
  28.  
  29. #include "application.h"
  30.  
  31. #include "stopwatch.h"
  32. #include "vretrace.h"
  33.  
  34. typedef struct settings
  35. {
  36.     short first_demo;
  37.     short last_demo;
  38.     short display_width;
  39.     short display_height;
  40.     short width;
  41.     short height;
  42.     short stripeSize;
  43.     short speed;
  44.     short speed_2;
  45.     short freq;
  46. } settings;
  47.  
  48. typedef void demoFunc( const settings &theSettings, fullscreen &full,
  49.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames);
  50.  
  51. demoFunc doTemporalTransparancy;
  52. demoFunc doHorizontalStripes;
  53. demoFunc doVerticalStripes;
  54. demoFunc doLSNRDemo;
  55. demoFunc doWGCheck1;
  56. demoFunc doWGCheck2;
  57.  
  58. #define numFuncs 6
  59.  
  60. demoFunc *theDemoFuncTable[ numFuncs] =
  61. {
  62.     doTemporalTransparancy,
  63.     doHorizontalStripes,
  64.     doVerticalStripes,
  65.     doLSNRDemo,
  66.     doWGCheck1,
  67.     doWGCheck2
  68. };
  69.  
  70. #define max(x,y) ((x) > (y)) ? (x) : (y)
  71. #define min(x,y) ((x) > (y)) ? (y) : (x)
  72.  
  73. void main()
  74. {
  75.     application me;
  76.  
  77.     Handle theResource = Get1Resource( 'Cnfg', 128);
  78.     settings theSettings;
  79.     
  80.     BlockMoveData( (settings *)*theResource, &theSettings, sizeof( settings));
  81.  
  82.     const int width                = theSettings.width;
  83.     const int height            = theSettings.height;
  84.     const int display_width        = theSettings.display_width;
  85.     const int display_height    = theSettings.display_height;
  86.  
  87.     ReleaseResource( theResource);
  88.  
  89.     const int first_demo = max( 0, theSettings.first_demo);
  90.     const int last_demo  = min( (numFuncs - 1), theSettings.last_demo);
  91.  
  92.     long the_millies[ numFuncs];
  93.     long the_frames[ numFuncs];
  94.     long the_freqs[ numFuncs];
  95.  
  96.     {
  97.         fullscreen full( 1);
  98.         Rect fullRect;
  99.         full.get_rect( &fullRect);
  100.  
  101.         const short vSize = fullRect.bottom - fullRect.top;
  102.         const short hSize = fullRect.right  - fullRect.left;
  103.     
  104.         const Rect origRect = {0, 0, height, width};
  105.         Rect destRect;
  106.         destRect.top    = (vSize - display_height) / 2;
  107.         destRect.left   = (hSize - display_width ) / 2;
  108.         destRect.bottom = destRect.top  + display_height;
  109.         destRect.right  = destRect.left + display_width;
  110.  
  111.         for( int stimType = first_demo; stimType <= last_demo; stimType += 1)
  112.         {
  113.             long num_millis = 0;
  114.             long num_frames = 0;
  115.             full.cls_black();
  116.             theDemoFuncTable[ stimType]( theSettings, full, origRect, destRect, &num_millis, &num_frames);
  117.             const long millis_per_frame =
  118.                     (num_frames == 0) ? 0 : (2 * num_millis + num_frames) / (2 * num_frames);
  119.             
  120.             the_millies[ stimType]    = num_millis;
  121.             the_frames[ stimType]    = num_frames;
  122.             the_freqs[ stimType]    = millis_per_frame;
  123.  
  124.             while( Button()){}
  125.         }
  126.     }
  127.     window v( 300, 30 + 20 * numFuncs);
  128.     v.use();
  129.     TextFont( monaco);
  130.     TextSize( 9);
  131.     TextMode( srcCopy);
  132.  
  133.     MoveTo(  10, 10);
  134.     DrawString( "\pframes");
  135.  
  136.     MoveTo( 110, 10);
  137.     DrawString( "\pms");
  138.  
  139.     MoveTo( 210, 10);
  140.     DrawString( "\pms/frame");
  141.  
  142.     short vPos = 30;
  143.  
  144.     for( int stimType = first_demo; stimType <= last_demo; stimType += 1)
  145.     {            
  146.         Str31 string;
  147.  
  148.         MoveTo(  10, vPos);
  149.         NumToString( the_frames[ stimType], string);
  150.         DrawString( string);
  151.  
  152.         MoveTo( 110, vPos);
  153.         if( the_millies[ stimType] > 0xFFFF)
  154.         {
  155.             DrawString( "\pOverflow in time counter");
  156.         } else {
  157.             NumToString( the_millies[ stimType], string);
  158.             DrawString( string);
  159.         }
  160.         MoveTo( 210, vPos);
  161.         NumToString( the_freqs[ stimType], string);
  162.         DrawString( string);
  163.  
  164.         vPos += 20;
  165.     }
  166.     while(  Button()){}
  167.     while( !Button()){}
  168.     while(  Button()){}
  169. }
  170.  
  171. void doTemporalTransparancy( const settings &theSettings, fullscreen &full,
  172.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  173. {
  174.     const int width            = theSettings.width;
  175.     const int height        = theSettings.height;
  176.     const int speed            = theSettings.speed;
  177.     const int speed_2        = theSettings.speed_2;
  178.     const int freq            = theSettings.freq;
  179.  
  180.     scrolling_noise H_pixels( width, height, 1, speed, speed_2, full);
  181.  
  182.     scrolling_noise V_pixels( width, height, 1, speed_2, speed, full);
  183.  
  184.     vretrace it( freq);
  185.     it.start();
  186.  
  187.     stopwatch omega;
  188.     omega.start();
  189.  
  190.     while( !Button())
  191.     {
  192.         it.sync();
  193.         full.copyfrom( H_pixels, origRect, destRect);
  194.         H_pixels.step();
  195.         *num_frames += 1;
  196.         
  197.         it.sync();
  198.         full.copyfrom( V_pixels, origRect, destRect);
  199.         V_pixels.step();
  200.         *num_frames += 1;
  201.     }
  202.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  203. }
  204.  
  205. void doHorizontalStripes( const settings &theSettings, fullscreen &full,
  206.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  207. {
  208.     const int width            = theSettings.width;
  209.     const int height        = theSettings.height;
  210.     const int stripeSize    = theSettings.stripeSize;
  211.     const int speed            = theSettings.speed;
  212.     const int speed_2        = theSettings.speed_2;
  213.     const int freq            = theSettings.freq;
  214.     //
  215.     // fill stripes with random pixels:
  216.     //
  217.     const int numstripes = height / stripeSize;
  218.  
  219.     scrolling_noise    toTheLeft( width, height / 2, 1,  -speed_2, -speed, full);
  220.     scrolling_noise    toTheRight( width, height / 2, 1, speed_2, speed, full);
  221.  
  222.     const Rect stripeRect = {0, 0, stripeSize, width};
  223.  
  224.     gworld total( width, height, 1);
  225.  
  226.     vretrace it( freq);
  227.     it.start();
  228.  
  229.     stopwatch omega;
  230.     omega.start();
  231.  
  232.     while( !Button())
  233.     {
  234.         //
  235.         // for every stripe, scroll it, scrolling in a new empty line,
  236.         // and then copy it into the 'total' gworld.
  237.         //
  238.         Rect orig = stripeRect;
  239.         Rect dest = stripeRect;
  240.  
  241.         toTheLeft.step();
  242.         toTheRight.step();
  243.         
  244.         for( int i = 0; i < numstripes; i++)
  245.         {
  246.             if( (i & 1) == 1)
  247.             {
  248.                 total.copyfrom( toTheLeft, orig, dest);
  249.                 OffsetRect( &orig, 0, stripeSize);
  250.             } else {
  251.                 total.copyfrom( toTheRight, orig, dest);
  252.             }
  253.             OffsetRect( &dest, 0, stripeSize);
  254.         }
  255.         //
  256.         // copy the result to the screen:
  257.         //
  258.         it.sync();
  259.         full.copyfrom( total, origRect, destRect);
  260.         *num_frames += 1;
  261.     }
  262.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  263. }
  264.  
  265. void doVerticalStripes( const settings &theSettings, fullscreen &full,
  266.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  267. {
  268.     const int width            = theSettings.width;
  269.     const int height        = theSettings.height;
  270.     const int stripeSize    = theSettings.stripeSize;
  271.     const int speed            = theSettings.speed;
  272.     const int speed_2        = theSettings.speed_2;
  273.     const int freq            = theSettings.freq;
  274.     //
  275.     // fill stripes with random pixels:
  276.     //
  277.     const int numstripes = width / stripeSize;
  278.  
  279.     scrolling_noise    toTheTop( width / 2, height, 1, speed, speed_2, full);
  280.     scrolling_noise    toTheBottom( width / 2, height, 1, -speed, -speed_2, full);
  281.  
  282.     const Rect stripeRect = {0, 0, height, stripeSize};
  283.  
  284.     gworld total( width, height, 1);
  285.  
  286.     vretrace it( freq);
  287.     it.start();
  288.  
  289.     stopwatch omega;
  290.     omega.start();
  291.  
  292.     while( !Button())
  293.     {
  294.         //
  295.         // for every stripe, scroll it, scrolling in a new empty line,
  296.         // and then copy it into the 'total' gworld.
  297.         //
  298.         Rect orig = stripeRect;
  299.         Rect dest = stripeRect;
  300.  
  301.         toTheTop.step();
  302.         toTheBottom.step();
  303.         
  304.         for( int i = 0; i < numstripes; i++)
  305.         {
  306.             if( (i & 1) == 1)
  307.             {
  308.                 total.copyfrom( toTheTop, orig, dest);
  309.                 OffsetRect( &orig, stripeSize, 0);
  310.             } else {
  311.                 total.copyfrom( toTheBottom, orig, dest);
  312.             }
  313.             OffsetRect( &dest, stripeSize, 0);
  314.         }
  315.         //
  316.         // copy the result to the screen:
  317.         //
  318.         it.sync();
  319.         full.copyfrom( total, origRect, destRect);
  320.         *num_frames += 1;
  321.     }
  322.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  323. }
  324.  
  325. void doLSNRDemo( const settings &theSettings, fullscreen &full,
  326.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  327. {
  328.     const int width        = theSettings.width;
  329.     const int height    = theSettings.height;
  330.     const int speed        = theSettings.speed;
  331.     const int speed_2    = theSettings.speed_2;
  332.     const int freq        = theSettings.freq;
  333.     //
  334.     // create the two one-bit worlds, a two-bit world
  335.     // containing the noise-degraded signal, and a world
  336.     // to put 'new noise' in.
  337.     //
  338.     gworld noiseWorld( width, height, 1);
  339.     scrolling_noise signalWorld( width, height, 1, speed, speed_2);
  340.  
  341.     CTabHandle theColors = (CTabHandle)Get1Resource( 'clut', 128);
  342.  
  343.     fullscreen fullTwo( 2, theColors);
  344.  
  345.     gworld totalWorld( width, height, 2, fullTwo);
  346.  
  347.     vretrace it( freq);
  348.     it.start();
  349.  
  350.     stopwatch omega;
  351.     omega.start();
  352.     
  353.     Rect fullRect;
  354.     fullTwo.get_rect( &fullRect);
  355.  
  356.     while( !Button())
  357.     {
  358.         //
  359.         // shift signal to the right a column, filling space with new noise
  360.         //
  361.         signalWorld.step();
  362.         //
  363.         // generate new noise, and merge it with the signal:
  364.         //
  365.         noiseWorld.fill_random();
  366.         
  367.         totalWorld.two_bit_merge( signalWorld, noiseWorld);
  368.         //
  369.         // copy the result to the screen:
  370.         //
  371. //        it.sync();
  372.         fullTwo.copyfrom( totalWorld, origRect, destRect);
  373.         //
  374.         // Adjust signal to noise ratio
  375.         //
  376.         // To get a LSNR of S we should solve for x from    (Note: this formula is bogus!!!)
  377.         //
  378.         //            (0x8000 + x) / (0x8000 - x) == S
  379.         //
  380.         //    <=>        x == 0x8000 * (S - 1) / (S + 1)
  381.         //
  382.         Point where;
  383.         GetMouse( &where);
  384.         
  385.         LocalToGlobal( &where);
  386.         
  387.         if( PtInRect( where, &fullRect))
  388.         {
  389.             static const Rect largeRect = {0, 0, 32767, 32767};
  390.             MapPt( &where, &fullRect, &largeRect);
  391.             const unsigned short noise = 2 * ((unsigned short)where.h);
  392.             const unsigned short signal = 0xFFFF - noise;
  393.             const unsigned short both = signal + noise;
  394.  
  395.             ColorSpec newColors[ 4] =
  396.             {
  397.                 {0, {0x0000, 0x0000, 0x0000}},        // black
  398.                 {1, {noise , noise , noise }},
  399.                 {2, {signal, signal, signal}},
  400.                 {3, {both  , both  , both  }}        // signal + noise
  401.             };
  402.             fullTwo.setentries( 0, 3, newColors);
  403.         }
  404.         *num_frames += 1;
  405.     }
  406.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  407.     ReleaseResource( (Handle)theColors);
  408. }
  409.  
  410. void doWGCheck1( const settings &theSettings, fullscreen &full,
  411.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  412. {
  413.     const int width            = theSettings.width;
  414.     const int height        = theSettings.height;
  415.     const int stripeSize    = theSettings.stripeSize;
  416.     const int speed            = theSettings.speed;
  417.     const int speed_2        = theSettings.speed_2;
  418.     const int freq            = theSettings.freq;
  419.  
  420.     CTabHandle theColors = (CTabHandle)Get1Resource( 'clut', 128);
  421.  
  422.     fullscreen fullTwo( 2, theColors);
  423.     fullTwo.cls_white();
  424.  
  425.     scrolling_noise signal( 4 * stripeSize, height, 1, speed, speed_2);
  426.     gworld mask( 4 * stripeSize, height, 1);
  427.     gworld total( 4 * stripeSize, height, 2, fullTwo);
  428.  
  429.     const Rect orig = {0, 0, height, 4 * stripeSize};
  430.  
  431.     const short destMid = (destRect.left + destRect.right) / 2;
  432.     const Rect dest = {destRect.top, destMid - 2 * stripeSize, destRect.top + height, destMid + 2 * stripeSize};
  433.  
  434.     const Rect topR = {0, 0, height / 2, 4 * stripeSize};
  435.     const Rect botR = {height / 2, 0, height, 4 * stripeSize};
  436.  
  437.     const Rect topR_half = {0, stripeSize, height / 2, 3 * stripeSize};
  438.     const Rect botR_half = {height / 2, stripeSize, height, 3 * stripeSize};
  439.  
  440.     mask.use();
  441.  
  442.     PaintRect( &topR);
  443.     EraseRect( &topR_half);
  444.  
  445.     EraseRect( &botR);
  446.     PaintRect( &botR_half);
  447.  
  448.     fullTwo.use();        // prevents a bug in vretrace.cp to crash the Mac
  449.     vretrace it( freq);
  450.     it.start();
  451.  
  452.     stopwatch omega;
  453.     omega.start();
  454.  
  455.     Rect fullRect;
  456.     fullTwo.get_rect( &fullRect);
  457.  
  458.     const unsigned short grys = 0x8000;
  459.     ColorSpec newColors[ 4] =
  460.     {
  461.         {0, {0, 0, 0}},
  462.         {1, {0, 0, 0}},
  463.         {2, {0, 0, 0}},        // = entire screen
  464.         {3, {0, 0, 0}}
  465.     };
  466.     const HSVColor grysHSV    = {0, 0, grys};
  467.  
  468.     HSV2RGB( &grysHSV, &newColors[ 0].rgb);
  469.     HSV2RGB( &grysHSV, &newColors[ 2].rgb);
  470.  
  471.     while( !Button())
  472.     {
  473.         signal.step();
  474.  
  475.         total.two_bit_merge( signal, mask);
  476.         it.sync();
  477.         fullTwo.copyfrom( total, orig, dest);
  478.         //
  479.         // Adjust signal strength:
  480.         //
  481.         Point where;
  482.         GetMouse( &where);
  483.         
  484.         LocalToGlobal( &where);
  485.         
  486.         if( PtInRect( where, &fullRect))
  487.         {
  488.             static const Rect largeRect = {0, 0, 32767, 32767};
  489.             MapPt( &where, &fullRect, &largeRect);
  490.             const unsigned short dark    = ((unsigned short)where.h);
  491.             const unsigned short light    = 0xFFFF - dark;
  492.  
  493.             const HSVColor darkHSV    = {0, 0, dark};
  494.             const HSVColor lightHSV    = {0, 0, light};
  495.             
  496.             HSV2RGB( &darkHSV, &newColors[ 3].rgb);
  497.             HSV2RGB( &lightHSV, &newColors[ 1].rgb);
  498.  
  499.             fullTwo.setentries( 0, 3, newColors);
  500.         }
  501.         *num_frames += 1;
  502.     }
  503.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  504.     ReleaseResource( (Handle)theColors);
  505. }
  506.  
  507. void doWGCheck2( const settings &theSettings, fullscreen &full,
  508.         const Rect &origRect, const Rect &destRect, long *num_millis, long *num_frames)
  509. {
  510.     const int width            = theSettings.width;
  511.     const int height        = theSettings.height;
  512.     const int stripeSize    = theSettings.stripeSize;
  513.     const int speed            = theSettings.speed;
  514.     const int speed_2        = theSettings.speed_2;
  515.     const int freq            = theSettings.freq;
  516.     //
  517.     // fill stripes with random pixels:
  518.     //
  519.     const int numstripes = width / stripeSize;
  520.  
  521.     scrolling_noise    toTheLeft( width, height, 1, speed, speed_2, full);
  522.  
  523.     const Rect stripeRect = {0, 0, height, stripeSize};
  524.  
  525.     gworld total( width, height, 1);
  526.  
  527.     vretrace it( freq);
  528.     it.start();
  529.  
  530.     stopwatch omega;
  531.     omega.start();
  532.  
  533.     while( !Button())
  534.     {
  535.         //
  536.         // for every stripe, scroll it, scrolling in a new empty line,
  537.         // and then copy it into the 'total' gworld.
  538.         //
  539.         Rect orig = stripeRect;
  540.         Rect dest = stripeRect;
  541.  
  542.         OffsetRect( &dest, width - stripeSize, 0);
  543.  
  544.         toTheLeft.step();
  545.  
  546.         for( int i = 0; i < numstripes; i++)
  547.         {
  548.             total.copyfrom( toTheLeft, orig, dest);
  549.             OffsetRect( &orig,  stripeSize, 0);
  550.             OffsetRect( &dest, -stripeSize, 0);
  551.         }
  552.         //
  553.         // copy the result to the screen:
  554.         //
  555.         it.sync();
  556.         full.copyfrom( total, origRect, destRect);
  557.         *num_frames += 1;
  558.     }
  559.     *num_millis = (long)stopwatch::milliseconds( omega.stop());
  560. }
  561.